package com.hwadee.teachingprocessmanagementsystem.controller.student;

import com.hwadee.teachingprocessmanagementsystem.dto.student.StudentCourseInfoDto;
import com.hwadee.teachingprocessmanagementsystem.dto.student.StudentCourseStandardDto;
import com.hwadee.teachingprocessmanagementsystem.dto.student.StudentCoursewareDto;
import com.hwadee.teachingprocessmanagementsystem.dto.student.StudentScheduleInfoDto;
import com.hwadee.teachingprocessmanagementsystem.entity.Course;
import com.hwadee.teachingprocessmanagementsystem.entity.Major;
import com.hwadee.teachingprocessmanagementsystem.entity.Student;
import com.hwadee.teachingprocessmanagementsystem.service.base.CourseService;
import com.hwadee.teachingprocessmanagementsystem.service.base.MajorService;
import com.hwadee.teachingprocessmanagementsystem.service.student.StudentService;
import com.hwadee.teachingprocessmanagementsystem.util.MD5Utils;
import com.hwadee.teachingprocessmanagementsystem.util.result.DataResult;
import org.apache.ibatis.annotations.Param;
import org.aspectj.apache.bcel.classfile.Module;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URLEncoder;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;

/**
 * @Description: 教学基础信息控制器，负责学生教学基础信息的请求
 * @Author: Jukomu
 * @Package: com.hwadee.teachingprocessmanagementsystem.controller.student
 * @Project: TeachingProcessManagementSystem
 * @name: TeachingDataController
 * @Date: 2024/7/12-9:00
 * @Filename: TeachingDataController
 */
@RestController("StudentTeachingDataController")
@RequestMapping("student/teaching-data")
public class TeachingDataController {
    @Autowired
    private MajorService majorService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private CourseService courseService;

    /**
     * 培养方案接口，返回培养方案pdf
     *
     * @return
     */
    @RequestMapping("talent-plans")
    public ResponseEntity<Resource> talentPlans(@RequestParam("student-number") String studentNumber) {
        // 获取学号前4位的专业编号
        String majorCode = studentNumber.substring(0, 4);
        // 根据专业编号查询培养方案
        Major major = majorService.queryById(Integer.parseInt(majorCode));
        try {
            // 获取pdf文件路径
            Path pdfPath = Paths.get("static/talent-plan/" + majorCode + "/" + major.getTeachingPlan());
            Resource resource = new ClassPathResource(pdfPath.toString());
            HttpHeaders headers = new HttpHeaders();
            headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=talent-plan.pdf");
            return new ResponseEntity<>(resource, headers, HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

    /**
     * 课程信息接口
     *
     * @return
     */
    @RequestMapping("course-info")
    public DataResult coursesInfo(@RequestParam("student-number") String studentNumber) {
        Student student = new Student();
        student.setStudentNumber(studentNumber);
        StudentCourseInfoDto studentCourseInfoDto = this.studentService.getStudentCourseInfoDto(student);
        return DataResult.successByData(studentCourseInfoDto);
    }

    /**
     * 学生课程标准接口
     * @param studentNumber
     * @return
     */
    @RequestMapping("materials/course-standard")
    public DataResult CourseStandard(@RequestParam("student-number") String studentNumber,
                                     @RequestParam("department") String department,
                                     @RequestParam("courseId") String courseId,
                                     @RequestParam("courseName") String courseName) {
        if ((!Objects.equals(department, "null") && !department.isEmpty()) || (!Objects.equals(courseId, "null") && !courseId.isEmpty()) || (!Objects.equals(courseName, "null") && !courseName.isEmpty())) {
            return DataResult.successByData(this.studentService.getStudentCourseStandardDto(department,courseName,courseId));
        }

        Student student = new Student();
        student.setStudentNumber(studentNumber);
        StudentCourseStandardDto studentCourseStandardDto = this.studentService.getStudentCourseStandardDto(student);
        return DataResult.successByData(studentCourseStandardDto);
    }



    /**
     * 教学计划接口
     *
     * @return
     */
    @RequestMapping("materials/teaching-plan")
    public DataResult teachingPlan() {
        return DataResult.succ();
    }

    /**
     * 课件接口
     *
     * @return
     */
    @RequestMapping("materials/courseware")
    public DataResult courseWareView(@RequestParam("student-number") String studentNumber) throws IOException {
        Student student = new Student();
        student.setStudentNumber(studentNumber);
        StudentCoursewareDto studentCourseWareDto = this.studentService.getStudentCourseWareDto(student);
        return DataResult.successByData(studentCourseWareDto);
    }

    @RequestMapping("materials/get-courseware")
    public ResponseEntity<Resource> getCourseWare(@RequestParam("dir") String dir,
                                                  @RequestParam("fileName") String fileName,
                                                  @RequestParam("courseId") String courseId) {
        // url构成：课程编号/教师编号/目录.../文件名
        String path = "src/main/resources/static/courseware/" + courseId + dir + "/" + fileName;

        try {
            Path filePath = Paths.get(path).toAbsolutePath().normalize();
            Resource resource = new UrlResource(filePath.toUri());

            if (resource.exists()) {
                return ResponseEntity.ok()
                        .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
                        .body(resource);
            } else {
                return ResponseEntity.notFound().build();
            }
        } catch (MalformedURLException e) {
            return ResponseEntity.badRequest().build();
        }
    }


    /**
     * 教师信息查询接口
     *
     * @return
     */
    @RequestMapping("teacher-info")
    public DataResult teacherInfo() {
        return DataResult.succ();
    }

    /**
     * 课表查询接口
     *
     * @return
     */
    @RequestMapping("schedule-info")
    public DataResult scheduleInfo(@RequestParam("student-number") String studentNumber) {
        Student student = new Student();
        student.setStudentNumber(studentNumber);
        StudentScheduleInfoDto studentScheduleInfoDto = this.studentService.getStudentScheduleInfoDto(student);
        return DataResult.successByData(studentScheduleInfoDto);
    }
}
